#include "lvgl/lvgl.h"
#include "lvgl/demos/lv_demos.h"
#include "lv_drivers/display/fbdev.h"
#include "lv_drivers/indev/evdev.h"
#include "./usrCode/ui.h"
#include "./usrCode/snake.h"
#include "./usrCode/snake_data.h"
#include <unistd.h>
#include <pthread.h>
#include <time.h>
#include <sys/time.h>
#include <stdlib.h>
#include <stdio.h>

#define DISP_BUF_SIZE (480 * 1024)

// 创建LVGL定时器
lv_timer_t * game_timer = NULL;
snake_head_p snake_head = NULL;


void game_timer_callback(lv_timer_t * timer)
{

    // 你的游戏逻辑代码在这里执行
    switch(screen_state_enum){
        case screen_game:
            snake_head = snake_init();
            screen_state_enum = screen_game_playing;
            lv_timer_set_period(game_timer, 500 - (game_speed_period * 4));
            printf("3. game speed %d\n", 500 - (game_speed_period * 4));
            rank_init_flag = 0;
            printf("1. to playing\n");
            break;
        case screen_game_playing:
            snake_move(snake_head);
            rank_init_flag = 0;
            break;
        case screen_rank:
            if(rank_init_flag == 0){
                rank_init();
                rank_init_flag = 1;
            }
            break;
        case screen_over:
            game_save_score();
            break;
        case screen_setting:
            break;
        default: break;
    }
}


int main(void)
{
    /*lvgl初始化*/
    lv_init();

    /*输出设备初始化及注册*/
    fbdev_init();
    /*A small buffer for LittlevGL to draw the screen's content*/
    static lv_color_t buf[DISP_BUF_SIZE];
    /*Initialize a descriptor for the buffer*/
    static lv_disp_draw_buf_t disp_buf;
    lv_disp_draw_buf_init(&disp_buf, buf, NULL, DISP_BUF_SIZE);
    /*Initialize and register a display driver*/
    static lv_disp_drv_t disp_drv;
    lv_disp_drv_init(&disp_drv);
    disp_drv.draw_buf   = &disp_buf;
    disp_drv.flush_cb   = fbdev_flush;
    disp_drv.hor_res    = 800;
    disp_drv.ver_res    = 480;
    lv_disp_drv_register(&disp_drv);

    //输入设备初始化及注册
    evdev_init();
    static lv_indev_drv_t indev_drv_1;
    lv_indev_drv_init(&indev_drv_1); /*Basic initialization*/
    indev_drv_1.type = LV_INDEV_TYPE_POINTER;
    /*This function will be called periodically (by the library) to get the mouse position and state*/
    indev_drv_1.read_cb = evdev_read;
    lv_indev_t *mouse_indev = lv_indev_drv_register(&indev_drv_1);

    //用户初始化
    ui_init();
    game_data_save_enum = game_data_save_none;
    game_gesture_count_enum = game_gesture_over_count;
    rank_init_flag = 0;

    game_timer = lv_timer_create(game_timer_callback, 500, NULL); // 500ms周期
    lv_timer_set_repeat_count(game_timer, -1); // 无限循环    

    /*事物处理及告知lvgl节拍数*/
    while(1) {      
        lv_timer_handler();//事务处理
        lv_tick_inc(5);//节拍累计
        usleep(5000);
    }
    return 0;
}


/*用户节拍获取*/
uint32_t custom_tick_get(void)
{
    static uint64_t start_ms = 0;
    if(start_ms == 0) {
        struct timeval tv_start;
        gettimeofday(&tv_start, NULL);
        start_ms = (tv_start.tv_sec * 1000000 + tv_start.tv_usec) / 1000;
    }

    struct timeval tv_now;
    gettimeofday(&tv_now, NULL);
    uint64_t now_ms;
    now_ms = (tv_now.tv_sec * 1000000 + tv_now.tv_usec) / 1000;

    uint32_t time_ms = now_ms - start_ms;
    return time_ms;
}
